/*
 * BTAuto - Android Location based Wifi Manager
 * Copyright (C) 2009 Bryan Emmanuel
 * 
 * This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 *  Bryan Emmanuel piusvelte@gmail.com
 */
package com.piusvelte.btauto;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.preference.PreferenceActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class UI extends PreferenceActivity implements CompoundButton.OnCheckedChangeListener, ServiceConnection {
	private Boolean mBtEnabled;
	private CheckBox mBtn_bt;
	private IBtAutoService mIBtAutoService;
	private final IUI mIUI = new IUI.Stub() {
		@Override
		public void bluetoothEnabled(boolean enabled) throws RemoteException {
			mBtEnabled = enabled;
			mBtn_bt.setChecked(enabled);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getPreferenceManager().setSharedPreferencesName(getString(R.string.key_preferences));
		addPreferencesFromResource(R.xml.preferences);
		setContentView(R.layout.main);
		mBtn_bt = (CheckBox) findViewById(R.id.btn_bt);
		mBtn_bt.setOnCheckedChangeListener(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		if (mIBtAutoService != null) {
			try {
				mIBtAutoService.setCallback(null);
			} catch (RemoteException e) {}
		}
		unbindService(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		if ((getSharedPreferences(getString(R.string.key_preferences), MODE_PRIVATE)).getBoolean(getString(R.string.key_enableservice), false)) startService(new Intent(this, BtAutoService.class));
		bindService(new Intent(this, BtAutoService.class), this, BIND_AUTO_CREATE);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if ((isChecked != mBtEnabled) && (mIBtAutoService != null)) {
			try {
				mIBtAutoService.enableBluetooth(isChecked);
			} catch (RemoteException e) {}
			// reverse the request and wait for the service to respond success or failure
			mBtn_bt.setChecked(isChecked);
		}
	}

	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
		mIBtAutoService = IBtAutoService.Stub.asInterface((IBinder) service);
		try {
			mIBtAutoService.setCallback(mIUI.asBinder());
		} catch (RemoteException e) {}
	}

	@Override
	public void onServiceDisconnected(ComponentName name) {
		mIBtAutoService = null;
	}

}